package com.xmqian.app.project.ui.user.activity

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.Button
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Switch
import com.coszero.common.base.mvp.BaseMvpActivity
import com.coszero.ui.dialog.ToastUtils
import com.coszero.ui.widget.CountDownTextView
import com.example.xmqian.R
import com.example.xmqian.databinding.ActivityRegisterBinding
import com.xmqian.app.project.ui.main.MainActivity
import com.xmqian.app.project.ui.user.contract.RegisterContract
import com.xmqian.app.project.ui.user.presenter.RegisterPresenter

/**
 * @author xmqian
 * @date 2019/5/18 12:18
 * @desc 用户注册
 */
class RegisterActivity : BaseMvpActivity<RegisterPresenter?>(), RegisterContract.View {
    var mTvGetCode: CountDownTextView? = null
    var mBtnReg: Button? = null
    var mEtPhone: EditText? = null
    var mEtPwd: EditText? = null
    var mEtSmsCode: EditText? = null
    var mSwAgree: Switch? = null

    /*输入字符监听*/
    var textWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            setRegBtnState()
        }

        override fun afterTextChanged(s: Editable) {
        }
    }
    private var binding: ActivityRegisterBinding? = null

    override fun getLayoutView(): LinearLayout {
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        return binding!!.root
    }

    override fun initView() {
        mTvGetCode = binding!!.tvGetCode
        mBtnReg = binding!!.btnReg
        mEtPhone = binding!!.etPhone
        mEtPwd = binding!!.etPwd
        mEtSmsCode = binding!!.etSmsCode
        mSwAgree = binding!!.swAgree
        title = "注册"
        mEtPwd!!.addTextChangedListener(textWatcher)
        mEtPhone!!.addTextChangedListener(textWatcher)
        mEtSmsCode!!.addTextChangedListener(textWatcher)

        mSwAgree!!.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean -> setRegBtnState() }
    }

    override fun initData() {
    }

    val smsCode: Unit
        get() {
            if (mEtPhone!!.length() == 11) {
                /* new PicCodeDialog.Builder(RegisterActivity.this)
                          .setPhoneNum(mEtPhone.getText().toString())
                          .setListener(dialog -> {
                              dialog.dismiss();
                              mTvGetCode.startCountDown(59);
                          })
                          .setType(PicCodeDialog.Builder.TYPE_REGISTER)
                          .build()
                          .show();*/
            } else {
                ToastUtils.showMsg("手机号长度不正确")
            }
        }

    /**
     * 立即注册
     */
    fun register() {
        mPresenter!!.register(
            mEtPhone!!.text.toString(),
            mEtPwd!!.text.toString(),
            mEtSmsCode!!.text.toString()
        )
    }

    /**
     * 获取用户协议
     */
    fun onClick(view: View) {
        if (view.id == R.id.tv_jump_login) { // 注册成功，跳转登录
            LoginActivity.Companion.startTask(this)
        } else if (view.id == R.id.tv_user_protocol) { // 用户协议
            // startActivity(MyWebViewFragment.getIntent(this, MyWebViewFragment.TYPE_SINGLE_PAGE, 8));
        } else if (view.id == R.id.tv_get_code) { // 获取验证码
            smsCode
        } else if (view.id == R.id.btn_reg) { // 注册
            register()
        }
    }


    /**
     * 设置注册按钮的启用状态
     */
    private fun setRegBtnState() {
        if (isUserInputDataIsValid) {
            mBtnReg!!.isEnabled = true
        } else {
            mBtnReg!!.isEnabled = false
        }
    }

    val isUserInputDataIsValid: Boolean
        /**
         * 用户输入的内容是否达到条件要求
         *
         * @return
         */
        get() = mEtPwd!!.length() >= 6 && mEtPhone!!.length() == 11 && mEtSmsCode!!.length() >= 4 && mSwAgree!!.isChecked

    //点击小眼睛 是否显示密码相关
    fun toggleState(isChecked: Boolean) {
        if (isChecked) {
            //明文密码
            mEtPwd!!.transformationMethod =
                HideReturnsTransformationMethod.getInstance()
            mEtPwd!!.setSelection(mEtPwd!!.text.toString().length)
        } else {
            //显示密码
            mEtPwd!!.transformationMethod =
                PasswordTransformationMethod.getInstance()
            mEtPwd!!.setSelection(mEtPwd!!.text.toString().length)
        }
    }

    override fun onRegisterSucceed(message: String?) {
        mPresenter!!.doLogin(mEtPhone!!.text.toString(), mEtPwd!!.text.toString())
    }

    override fun onRequestFailed(message: String?) {
        ToastUtils.showMsg(message)
    }

    override fun onLoginSucceed(message: String?) {
        ToastUtils.showMsg(message)
        startActivity(Intent(this, MainActivity::class.java))
    }

    override fun onNetworkError() {
    }

    override fun getPresenter(): RegisterPresenter {
        return RegisterPresenter(this)
    }
}
